/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.opentoast.maven;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.Manifest;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

/**
 * @author <a href="mailto:jiwhiz@gmail.com">Yuan Ji</a>
 *
 */
public abstract class AbstractManifestWriter 
{
    private static final String MANIFEST_MF_FILENAME = "MANIFEST.MF";

    private static final String META_INF_DIRECTORY = "META-INF";

    protected Log log;
    
    protected File metaInfBaseDirectory;
    
    protected MavenProject project;
    
    public void init(Log log, MavenProject project, File metaInfBaseDirectory)
    {
        this.log = log;
        this.project = project;
        this.metaInfBaseDirectory = metaInfBaseDirectory;
    }
    
    public File getManifestFile()
    {
        return new File( metaInfBaseDirectory, META_INF_DIRECTORY + File.separatorChar + MANIFEST_MF_FILENAME );
    }

    public void write() throws MojoExecutionException
    {
        File manifestFile = getManifestFile();
        Manifest manifest = createNewManifest();

        log.info( "MANIFEST LOCATION: " + manifestFile );

        try
        {
            writeManifest(manifest, manifestFile);
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( "Error trying to write Manifest to file " + manifestFile, e );
        }
    }
    
    protected void writeManifest( Manifest manifest, File outputFile ) throws IOException
    {
        outputFile.getParentFile().mkdirs();

        FileOutputStream os;
        os = new FileOutputStream( outputFile );
        try
        {
            manifest.write( os );
        }
        finally
        {
            try
            {
                os.close();
            }
            catch ( IOException e )
            {
                // nothing we can do here
            }
        }
    }
    
    abstract protected Manifest createNewManifest() throws MojoExecutionException;

}
